package com.siqi.hr.system.custom.dao.mysql;

import com.siqi.hr.system.common.sql.RelatedConfig;
import com.siqi.hr.system.common.sql.RelatedTable;
import com.siqi.hr.system.common.utils.Common;
import com.siqi.hr.system.custom.dao.intf.DbStructureDao;
import com.siqi.hr.system.custom.db.builder.DbTableAlter;
import com.siqi.hr.system.custom.db.builder.DbTableCreater;
import com.siqi.hr.system.custom.db.builder.DbTableDeleter;
import com.siqi.hr.system.custom.db.builder.RelatedAlter;
import com.siqi.hr.system.custom.db.builder.intf.DbCreater;
import com.siqi.hr.system.custom.entity.EntityRecord;
import com.siqi.hr.system.custom.entity.FieldRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * DB操作(也就是实体字段添加)dao实现类层
 * date                      2016/8/30.
 * @author                  xiaogq
 * @since JDK1.8.0_12
 */
@Component("dbStructureDao")
public class DbStructureDaoImpl implements DbStructureDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int build(DbCreater create) {
        return create.create(this.jdbcTemplate);
    }

    public int excuteDbOperateWithExceptionHandle(DbCreater creater) {
        int lens = 0;
        try {
            lens = this.build(creater);
        } catch (Exception e) {
            e.printStackTrace();
            creater.rollbackDbException();
        }
        return lens;
    }

    @Override
    public int addEntity(final EntityRecord entity) {
        DbTableCreater d = new DbTableCreater(entity) {
            public void preparationCreateStal() {
                FieldRecord ff = null;
                this.fields.add(new FieldRecord(entity,"create_date_datetime", "创建时间", true, true));
                this.fields.add(new FieldRecord(entity,"update_date_timestamp", "更新时间", true, true));
                this.fields.add(new FieldRecord(entity, "pk_t_3_1","记录创建者", true, true));
                this.fields.add(new FieldRecord(entity, "pk_t_3_2","记录更新者", true, true));
                //如果是受权限控制实体(xiaogq)
                if(entity.getPermission()){
                    this.fields.add(new FieldRecord(entity, "pk_t_3_3","记录拥有者", true, false));
                    this.fields.add(new FieldRecord(entity, "pk_t_2_1", "部门",true,false));
                }
                FieldRecord flagb = new FieldRecord(entity, "flag_bit", "停用标记", true,true);
                flagb.setHiddenShow(true);
                flagb.setListShow(false);
                flagb.setDefaultValue("1");
                this.fields.add(flagb);
                //如果是有回收站实体
                if (entity.getUseRecyclebin() != null && entity.getUseRecyclebin()) {
                    FieldRecord vb = new FieldRecord(entity, "valid_data_bit", "有效数据", true,true);
                    vb.setHiddenShow(true);
                    vb.setListShow(false);
                    vb.setDefaultValue("1");
                    this.fields.add(vb);
                }
                //如果是有实体类型
                if (entity.getDefineType() != null && entity.getDefineType()) {
                    FieldRecord et = new FieldRecord(entity,"pk_sys_custom_entity_type_1", "实体类型", true,true);
                    et.setReadOnly(true);
                    this.fields.add(et);
                }
                //如果实体可以定义审批流
                if (entity.getDefineApproval() != null && entity.getDefineApproval()) {
                    ff = new FieldRecord(entity, "approval_state_varchar", "审批状态", true,true);
                    this.fields.add(ff);
                    ff = new FieldRecord(entity, "pk_sys_custom_approval_period_1", "审批阶段", true,true);
                    this.fields.add(ff);
                    FieldRecord submitPerson = new FieldRecord(entity,"pk_t_3_10", "提交人", true,true);
                    this.fields.add(submitPerson);
                    FieldRecord submitDate = new FieldRecord(entity,"submit_datetime", "提交时间", true,true);
                    this.fields.add(submitDate);
                    FieldRecord approvalPerson = new FieldRecord(entity, "pk_t_3_11", "审批人", true,true);
                    this.fields.add(approvalPerson);
                    FieldRecord approvalDate = new FieldRecord(entity,"approval_datetime", "审批时间", true,true);
                    this.fields.add(approvalDate);
                    FieldRecord shengpiliu = new FieldRecord(entity,"pk_t_53_1", "审批流程", true,false);
                    shengpiliu.setReadOnly(true);
                    this.fields.add(shengpiliu);
                }
                this.fields.add(new FieldRecord(entity, "description_textarea","描述", true,255));
            }
        };
        return excuteDbOperateWithExceptionHandle(d);
    }

    @Override
    public void deleteEntity(Integer entityId) {
        EntityRecord entity = new EntityRecord(entityId);
        DbTableDeleter d = new DbTableDeleter(entity);
        excuteDbOperateWithExceptionHandle(d);
    }

    @Override
    public void addField(FieldRecord fieldRecord) {
        //添加表结构字段、往注册表中添加字段记录方法(xiaogq)
        excuteDbOperateWithExceptionHandle(new DbTableAlter(fieldRecord).getDbCreater(DbTableAlter.TableAlterType.field_add));
        //如果添加的是引用字段(则还的添加相关对象关系)
//        if (fieldRecord.parse().isQuote()) {
//            //如果不是实体自己引用自己，则添加两个实体关系表
//            if (fieldRecord.getQuoteEntity().getTableCode() != fieldRecord.getEntityRecord().getTableCode()){
//                //定义相关项关系对象
//                RelatedConfig rc = new RelatedConfig(RelatedTable.RELATED_HEAD+fieldRecord.getQuoteEntity().getTableCode()+"_"+fieldRecord.getEntityRecord().getTableCode(),fieldRecord.getSuffix());
//                //添加相关项关系表
//                excuteDbOperateWithExceptionHandle(new RelatedAlter(rc).getDbCreater(RelatedAlter.RelatedAlterType.related_add));
//            }
//        }
    }

    @Override
    public void deleteField(FieldRecord fieldRecord) {
        //删除表结构字段、往注册表中添加字段记录方法(xiaogq)
        excuteDbOperateWithExceptionHandle(new DbTableAlter(fieldRecord).getDbCreater(DbTableAlter.TableAlterType.field_delete));
        try {
            //如果删除的是引用字段(则还的添加相关对象关系)
            if (fieldRecord.parse().isQuote()) {
                //获取引用字段后缀索引
                int suffix = Common.getFieldSuffix(fieldRecord.getName());
                RelatedConfig rc = new RelatedConfig(RelatedTable.RELATED_HEAD+fieldRecord.getQuoteEntity().getTableCode()+"_"+fieldRecord.getEntityRecord().getTableCode(),suffix);
                excuteDbOperateWithExceptionHandle(new RelatedAlter(rc).getDbCreater(RelatedAlter.RelatedAlterType.related_removed));
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

}
